package com.git.smp.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.git.smp.core.PageRequest;
import com.git.smp.core.ResponseMessage;
import com.git.smp.dao.SmpFileMapper;
import com.git.smp.entity.SmpFile;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@RestController
@RequestMapping("/help")
public class SmpHelpController {
	
	Logger logger = LoggerFactory.getLogger(SmpHelpController.class);

	@Autowired
	SmpFileMapper smpFileMapper;
	
	 
	   /**
	   * 添加文档
	   * @param file
	   * @param fileName
	   * @param fileDescribe
	   * @return
	   */
	  @RequestMapping("/addFile")
	  public ResponseMessage<?> addFile(
			  MultipartFile file,
			  @RequestParam(name = "fileName")  String fileName ,
			  @RequestParam(name="fileDescribe") String fileDescribe
	    	) {
		  	
		  	ResponseMessage<?> response = new ResponseMessage<>();
	        response.setCode("0000");
	        response.setMessage("添加文档成功！");
	        
		  	Date date = new Date();
		  	
		  	SmpFile smpFile = new SmpFile();
		  	
		  	try {
				smpFile.setFileContent(file.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  	smpFile.setFileDescribe(fileDescribe);
		  	smpFile.setFileName(file.getOriginalFilename());
		  	smpFile.setUpdateDate(date);
		  	smpFileMapper.insert(smpFile);
		  	
//	        logger.info("添加文档成功！");
			return response;
	    }
	  
	  
	  
	  @RequestMapping("/updateFile")
	  public ResponseMessage<?> updateFile(
			  MultipartFile file,
			  @RequestParam(name = "fileName")  String fileName ,
			  @RequestParam(name="fileDescribe") String fileDescribe
	    	) {
		  	
		  	ResponseMessage<?> response = new ResponseMessage<>();
	        response.setCode("0000");
	        response.setMessage("修改文档成功！");
	        
		  	Date date = new Date();
		  	
		  	SmpFile smpFile = new SmpFile();
		  	
		  	try {
				smpFile.setFileContent(file.getBytes());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		  	smpFile.setFileDescribe(fileDescribe);
		  	smpFile.setFileName(file.getOriginalFilename());
		  	smpFile.setUpdateDate(date);
		  	smpFileMapper.updateByPrimaryKeySelective(smpFile);
		  	
//	        logger.info("添加文档成功！");
			return response;
	    }
	  
	  
	  
	  /**
	   * 分页查询
	   * @param pageRequest
	   * @return
	   */
	  @RequestMapping("/getFileManageByPage")
	  public ResponseMessage<PageInfo<SmpFile>> getFileManageByPage(
			  @RequestBody PageRequest<SmpFile> pageRequest
	    	) {
	    	ResponseMessage<PageInfo<SmpFile>> responseMessage = new ResponseMessage<PageInfo<SmpFile>>();
	        responseMessage.setCode("0000");
	        responseMessage.setMessage("查询文档成功！");
	        
	        SmpFile smpFile =pageRequest.getBody();
	        
	        PageHelper.startPage(pageRequest.getPageNum(), pageRequest.getPageSize());
	        List<SmpFile> listSmpFile = smpFileMapper.getFileManageByPage(smpFile);
	        if (listSmpFile == null) {
	            responseMessage.setCode("0001");
	            responseMessage.setMessage("未查询到文档！");
	        }
	        PageInfo<SmpFile> pageInfo = new PageInfo<SmpFile>(listSmpFile, pageRequest.getPageSize());
	        responseMessage.setExtend(pageInfo);
	        logger.info(responseMessage.getMessage());
	        return responseMessage;
	    }
	  
	  
	  	/**
	  	 * 下载
	  	 * @param fileName
	  	 * @param response
	  	 */
	  	@RequestMapping(value = "/getFileDown")
		public void getDisplayModeImg(@RequestParam(value = "fileName") String fileName, HttpServletResponse response) {
			byte[] data = null;
			
			SmpFile smpFile = smpFileMapper.selectByPrimaryKey(fileName);

			data = smpFile.getFileContent();
//				response.setContentType("image/jpeg");
//		        response.setCharacterEncoding("UTF-8");
	        try {
	        	OutputStream outputSream = response.getOutputStream();
	            outputSream.write(data);
	            outputSream.flush();
	            logger.info("下载成功");
	        }catch(IOException e) {
	        	logger.error("下载失败！");
	            e.printStackTrace();
	        }
		}

}
